/*
 * Copyright 2018 Bakumon. https://github.com/Bakumon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package me.kingz.family.fiance.ui.home;

import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;

import me.drakeet.floo.Floo;
import me.drakeet.floo.StackCallback;
import me.kingz.family.fiance.R;
import me.kingz.family.fiance.Router;
import me.kingz.family.fiance.base.BaseActivity;
import me.kingz.family.fiance.databinding.ActivityHomeBinding;
import me.kingz.family.fiance.ui.wish.WishFragment;

/**
 * HomeActivity
 *
 * @author bakumon https://bakumon.me
 * @date 2018/4/9
 */
public class HomeActivity extends BaseActivity implements StackCallback {

    private static final String TAG = HomeActivity.class.getSimpleName();

    private ActivityHomeBinding mBinding;

    private HomeViewModel homeViewModel;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_home;
    }

    @Override
    protected void onInit(@Nullable Bundle savedInstanceState) {
        mBinding = getDataBinding();
        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);

//        homeViewModel.isDrawerOpened().observe(this,isOpened->{
//            if (isOpened)
//                mBinding.drawerLayout.openDrawer(Gravity.START);
//            else mBinding.drawerLayout.closeDrawer(Gravity.START);
//        });
        initView();
    }

    private WishFragment wishFragment;
    private void initView() {
        RecordFragment recordFragment = (RecordFragment) RecordFragment.instantiate(this, RecordFragment.class.getName());
        getSupportFragmentManager().beginTransaction().add(mBinding.contentPager.getId(), recordFragment).commit();

        setSupportActionBar(mBinding.toolBar);

        mBinding.menus.setNavigationItemSelectedListener(menuItem -> {
            switch (menuItem.getItemId()) {
                case R.id.records:
                    getSupportFragmentManager().beginTransaction().replace(mBinding.contentPager.getId(),recordFragment).commit();
                    break;
                case R.id.wish:
                    if(wishFragment==null)
                        wishFragment = new WishFragment();
                    getSupportFragmentManager().beginTransaction().replace(mBinding.contentPager.getId(),wishFragment).commit();
                    break;
                case R.id.statistics:
                    Floo.navigation(this, Router.Url.URL_STATISTICS).start();
                    break;
                case R.id.setting:
                    Floo.navigation(this, Router.Url.URL_SETTING).start();
                    break;
            }
            mBinding.drawerLayout.closeDrawer(Gravity.START);
            return true;
        });
    }

    @Nullable
    @Override
    public String indexKeyForStackTarget() {
        return Router.IndexKey.INDEX_KEY_HOME;
    }

    @Override
    public void onReceivedResult(@Nullable Object result) {
    }

    public void menuClick(View view) {
//        switchDrawer();
        if (mBinding.drawerLayout.isDrawerOpen(Gravity.START))
            mBinding.drawerLayout.closeDrawer(Gravity.START);
        else mBinding.drawerLayout.openDrawer(Gravity.START);
    }

}
